package com.ruoyi.web.controller.common;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    @Log(title = "上传文件", businessType = BusinessType.UPLOAD)
    public AjaxResult uploadMinio(@RequestPart MultipartFile file, String type) throws Exception {
        return AjaxResult.success("文件上传成功", fileService.uploadMinio(file, type));
    }

    @GetMapping("/download")
    @Log(title = "下载文件", businessType = BusinessType.DOWNLOAD)
    public void download(HttpServletResponse response, String name, String type) {
        fileService.download(response, name, type);
    }

    @GetMapping("/delete")
    @Log(title = "删除文件", businessType = BusinessType.REMOVE_FILE)
    public AjaxResult delete(String name, String type) throws Exception {
        return AjaxResult.status(fileService.delete(name, type), "成功删除文件", "删除文件失败");
    }

}
